<template>
  <div class="app-container">
    <div class="app-container_conter">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px" style="display: flex;align-items: flex-end;justify-content: space-between">
      <div>
      <el-form-item label="方案简介" prop="synopsis">
        <el-input
          v-model="queryParams.synopsis"
          placeholder="请输入方案简介"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="对应工种" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择对应工种" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      </div>
      <div style="width: 150px">
      <el-form-item style="width: 150px;text-align: right">
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="info"  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            style="background: #0793FF;color:#FFFFFF "
          size="mini"
          @click="handleAdd"
          v-hasPermi="['insurance:productType:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['insurance:productType:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="info"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['insurance:productType:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['insurance:productType:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table  v-loading="loading" :data="productTypeList" @selection-change="handleSelectionChange"  :header-cell-style="tableHeaderColor">
      <el-table-column type="selection" width="55" align="center"  />
      <el-table-column label="方案简介" align="center" min-width="130px" prop="synopsis" fixed />
      <el-table-column label="对应工种" align="center" min-width="120px" :show-overflow-tooltip="true">
        <template slot-scope="scope">
<!--          <router-link :to="{ path:'/product/type/data',query:{product:scope.row }}" class="link-type">-->
            <span v-for="dict in typeOptions" v-if="dict.dictValue === scope.row.type" >{{dict.dictLabel}}</span>
<!--          </router-link>-->
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" min-width="150px" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link style="margin-right: 10px" :to="{ path:'/product/type/data',query:{product:scope.row }}" class="link-type">
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['insurance:productType:edit']"
          >详情</el-button>
          </router-link>

          <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['insurance:productType:edit']"
          >修改</el-button>
          <el-button
              style="color:#FF0000 "
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['insurance:productType:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </div>
    <pagination
        style="background: transparent"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品方案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="方案简介" prop="synopsis">
          <el-input v-model="form.synopsis" type="textarea" maxlength="200" size="small" rows="5" show-word-limit  placeholder="请输入方案简介" />
        </el-form-item>
        <el-form-item label="对应工种" prop="type">
          <el-select v-model="form.type" placeholder="请选择对应工种">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button type="info" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProductType, getProductType, delProductType, addProductType, updateProductType, exportProductType,plan_type_list } from "@/api/insurance/productType";
import Template from "../../base/template/index";

export default {
  name: "ProductType",
  components: {
    Template
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品方案表格数据
      productTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 对应工种字典
      typeOptions: [],
      // 0 禁用 1激活字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        synopsis: null,
        type: null,
        status: null
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        synopsis: [{
          required: true, message: "简介简介不能为空", trigger: "blur"
        }],
        type: [{
          required: true, message: "方案类型不能为空", trigger: "blur"
        }]
      },

    };
  },
  created() {
    this.getList();
    this.getDicts("industry_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("common_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /* 修改表头颜色 */
    tableHeaderColor({ rowIndex }) {
      if (rowIndex === 0) {
        return 'background-color: #304156;color: #fff;font-weight: 400;font-family: Microsoft YaHei;'
      }
    },
    /** 查询产品方案列表 */
    getList() {

      // listProductType(this.queryParams).then(response => {
      //   console.log('response',response)
      //   // this.productTypeList = response.rows;
      //   // this.total = response.total;
      //
      // });
      this.loading = true;
      plan_type_list(this.queryParams).then(res=>{
        this.loading = false;
             // console.log('列表',res)
        if(res.code==1){
          this.productTypeList = res.data.list;
          this.total = res.data.count;
        }
      })
    },
    // 对应工种字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 0 禁用 1激活字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        synopsis: null,
        type: null,
        createTime: null,
        updateTime: null,
        status: "1"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品方案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品方案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductType(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductType(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除产品方案编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProductType(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品方案数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProductType(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style scoped>
.app-container_conter{
  background: #F8FBFF;
  padding:30px;
  border-radius: 10px;
  box-sizing: border-box;
}
</style>
